<?php
App::uses('AppController', 'Controller');
/**
 * Ratings Controller
 *
 * @property Rating $Rating
 */
class RatingsController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$rating_groups = $this->Rating->RatingGroup->find('list');
		$this->Rating->recursive = 0;
		$this->set('ratings', $this->paginate());
		$this->set('rating_groups', $rating_groups);
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Rating->id = $id;
		if (!$this->Rating->exists()) {
			throw new NotFoundException(__('Invalid rating'));
		}
		$this->set('rating', $this->Rating->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Rating->create();
			if ($this->Rating->save($this->request->data)) {
				$this->Session->setFlash(__('The rating has been saved'));				
				$rating_id = $this->Rating->getLastInsertID();
				if(isset($this->passedArgs['a']) && !empty($this->passedArgs['a'])){
					// update new Rating for Article
					$this->loadModel('Article');
					$this->Article->recursive = -1;
					$options['joins'] = array(
						array('table' => 'rating_articles',
								        'alias' => 'RatingArticle',
								        'type' => 'inner',
								        'conditions' => array(
								            'Article.id = RatingArticle.article_id'
						)
						),
						array('table' => 'ratings',
								        'alias' => 'Rating',
								        'type' => 'inner',
								        'conditions' => array(
								            'RatingArticle.rating_id = Rating.id'									
						)
						)
						);
					$options['conditions'] = array(
							    'Article.id' => $this->passedArgs['a']
					);
					$options['fields'] = array(
											    'Rating.id'
					);
					$article_ratings = array();
					$curent_ratings = $this->Article->find('all',$options);
					foreach ($curent_ratings as $key => $r) {
						$article_ratings['Rating'][$key] = $r['Rating']['id'];
					}
					// dodajemy do juz istniejacych nowe pytanie
					$article_ratings['Rating'][count($curent_ratings)+1] = $rating_id;
					$data = array('Article'=>array('id'=>$this->passedArgs['a']),'Rating'=>$article_ratings);
					$this->Article->id = $this->passedArgs['a'];
					$this->Article->save($data);
					
					 $this->redirect(array('controller'=>'articles','action' =>'view',$this->passedArgs['a']));
				}
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rating could not be saved. Please, try again.'));
			}
		}
		
		if(isset($this->passedArgs['a']) && !empty($this->passedArgs['a'])){
			// dodajemy do artykulu
			$this->loadModel('Article');
			$this->Article->id = $this->passedArgs['a'];
			if ($this->Article->exists()) {
				$data['Article']['id'] = $this->passedArgs['a'];
				$this->request->data = $data;
			}
		}
		
		$rating_groups = $this->Rating->RatingGroup->find('list');
		$this->set('ratingGroups', $rating_groups);
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Rating->id = $id;
		if (!$this->Rating->exists()) {
			throw new NotFoundException(__('Invalid rating'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Rating->save($this->request->data)) {
				$this->Session->setFlash(__('The rating has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rating could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Rating->read(null, $id);
			$rating_groups = $this->Rating->RatingGroup->find('list');
			$this->set('ratingGroups', $rating_groups);
		}
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Rating->id = $id;
		if (!$this->Rating->exists()) {
			throw new NotFoundException(__('Invalid rating'));
		}
		if ($this->Rating->delete()) {
			$this->Session->setFlash(__('Rating deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Rating was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	
	public function delete_value($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		
		if(isset($this->passedArgs['r']) && !empty($this->passedArgs['r'])){
			// dodajemy do artykulu
			$this->Rating->id = $this->passedArgs['r'];
			if ($this->Rating->exists()) {
				$this->Rating->RatingValue->id = $id;
				if (!$this->Rating->RatingValue->exists()) {
					throw new NotFoundException(__('Invalid rating'));
				}
				if ($this->Rating->RatingValue->delete()) {
					$this->Session->setFlash(__('Rating deleted'));
					$this->redirect(array('controller'=>'ratings','action' => 'view',$this->passedArgs['r']));
				}
			}
		}
		

		$this->Session->setFlash(__('Rating was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
